<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-menu"></i> 表格</el-breadcrumb-item>
                <el-breadcrumb-item>基础表格</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-button type="primary" icon="el-icon-circle-plus-outline" class="handle-del mr10" @click="addBlog">新增</el-button>
            <el-button type="primary" icon="el-icon-refresh" class="handle-del mr10" @click="removeCache">清理缓存</el-button>
            <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
          <!--  <el-select v-model="searchParams.tagId" placeholder="筛选省份" class="handle-select mr10">
                <el-option key="1" label="广东省" value="广东省"></el-option>
                <el-option key="2" label="湖南省" value="湖南省"></el-option>
            </el-select>-->
            <el-input v-model="searchParams.title" placeholder="筛选关键词" class="handle-input mr10"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </div>
        <el-table :data="tableData" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="title" label="博客标题"  width="320">
            </el-table-column>
            <el-table-column prop="author" label="作者" width="120">
            </el-table-column>
            <el-table-column   label="标签" width="320">
                <template scope="scope">
                    <el-button size="small" v-for="tag in scope.row.tags"
                               >{{tag.tagText}}</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="touchZan" label="点赞数" sortable width="120">
            </el-table-column>
            <el-table-column prop="browseCount" label="浏览量" sortable width="120">
            </el-table-column>

            <el-table-column label="操作" width="180">
                <template scope="scope">
                    <el-button size="small"
                               @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger"
                               @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                @current-change ="handleCurrentChange"
                layout="prev, pager, next"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import router from '../../router';

    export default {
        data() {
            return {
                url: '/api/cloud-blog-web/blogs',
                tableData: [],
                total: 0,
                searchParams:{
                    pageNumber : 1,
                    pageSize : 10,
                    tagId: '',
                    title :''
                },
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false
            }
        },
        created(){
            this.getData();
        },
        computed: {

        },
        methods: {
            removeCache() {
                this.$axios.post('/api/cloud-blog-web/blog/cache/remove')
                this.$message.success('清理完成');

            },
            // 新增博客
            addBlog(){
                router.push('/add/blog')
            },
            handleCurrentChange(val){
                this.searchParams.pageNumber = val;
                this.getData();
            },
            getData(){
                let self = this;
                self.$axios.post(self.url, self.searchParams).then((res) => {
                    self.tableData = res.data.records;
                    self.total = res.data.total;
                })
            },
            search(){
                this.searchParams.pageNumber = 1;
                this.getData();
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                router.push({ path: '/add/blog', query:{ id: row.id }})
            },
            handleDelete(index, row) {
                this.$message.error('删除第'+(index+1)+'行');
            },
            delAll(){
                const self = this,
                    length = self.multipleSelection.length;
                let str = '';
                self.del_list = self.del_list.concat(self.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += self.multipleSelection[i].name + ' ';
                }
                self.$message.error('删除了'+str);
                self.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }
</script>

<style scoped>
    .handle-box{
        margin-bottom: 20px;
    }
    .handle-select{
        width: 120px;
    }
    .handle-input{
        width: 300px;
        display: inline-block;
    }
</style>
